//
//  AYNewFeatureViewController.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/14.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit

/// 重用标识
private let reuseIdentifier = "cell"

/// item的个数
private let itemCount = 4


class AYNewFeatureViewController: UICollectionViewController {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
        /// flowLyout必须要有
    private let layout = UICollectionViewFlowLayout()
    
    
    init() {
        super.init(collectionViewLayout: layout)
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        preparCollectionView()
        
    }
    
    
    private func preparCollectionView() {
        
        //代码创建必须得先注册cell
        collectionView?.registerClass(AYNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        collectionView?.bounces = false
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        collectionView?.pagingEnabled = true
    }
}




extension AYNewFeatureViewController {
    
    // MARK: - 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! AYNewFeatureCell
        
        
        cell.index = indexPath.item
        
        cell.backgroundColor = UIColor.grayColor()
        
        return cell
    }
    
    
    //代理方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
         let index = Int(scrollView.contentOffset.x / scrollView.frame.width)
        
        if index == itemCount - 1 {
            
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! AYNewFeatureCell
            cell.startAnimation()
            
        }
        
    }
    
    
    
    
    
    
}